package com.alpaak.bootdemo.alpaak.aspect;

import com.alpaak.bootdemo.alpaak.annotation.Action;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author liudongkui
 * @desp
 * @datetime 2019/10/18 9:06
 */
@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.alpaak.bootdemo.alpaak.annotation.Action)")
    public void annotationPointCut(){}

    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("拦截的方法："+method.getName());
        System.out.println("注释式拦截:"+action.name());
    }

    @Before("annotationPointCut()")
    public void annoBefore(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("注释式拦截:"+action.name());
    }

    @Before("execution(* com.alpaak.bootdemo.alpaak.service.DemoMothodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规式拦截"+method.getName());
    }
}
